#include "AppDelegate.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
#include "CCEventType.h"
#include "GondorScene.h"

#define  LOG_TAG    "main"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

using namespace cocos2d;

extern "C"
{

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JniHelper::setJavaVM(vm);

    return JNI_VERSION_1_4;
}

void Java_com_zondig_gandalf_gandalf_serverConnected(JNIEnv* env, jobject thiz)
{
	GondorScene::connected = true;
}

//gandalf.java (playerShot)
void Java_com_zondig_gandalf_gandalf_playerShot(JNIEnv*  env, jobject thiz, jint x, jint y, jint wo, jint ho)
{
	if(GondorScene::activeGondor==NULL){
		return;
	}

	GondorScene::activeGondor->gotFire(x, y, wo, ho);
}

void Java_com_zondig_gandalf_gandalf_creatureArray(JNIEnv* env, jobject thiz, jintArray arr){
    if(GondorScene::activeGondor == NULL){
        return;
    }

    jsize count = env->GetArrayLength(arr);
    jint *numbers = env->GetIntArrayElements(arr, NULL);

    GondorScene::activeGondor->fillPaths(numbers, count);
}

void Java_com_zondig_gandalf_gandalf_playerLeft(JNIEnv* env, jobject thiz)
{
	if(GondorScene::activeGondor == NULL){
		return;
	}

	GondorScene::activeGondor->playerLeft();
}

void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv*  env, jobject thiz, jint w, jint h)
{
    if (!CCDirector::sharedDirector()->getOpenGLView())
    {
        CCEGLView *view = CCEGLView::sharedOpenGLView();
        view->setFrameSize(w, h);

        AppDelegate *pAppDelegate = new AppDelegate();
        CCApplication::sharedApplication()->run();
    }
    else
    {
        ccDrawInit();
        ccGLInvalidateStateCache();
        
        CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
        CCTextureCache::reloadAllTextures();
        CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);
        CCDirector::sharedDirector()->setGLDefaultValues(); 
    }
}

}
